Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			 {name:'id',type:'string'},
             {name:'employe_id',type:'string'},
             {name:'employe__name',type:'string'},
             {name:'month',type:'string'},
             {name:'a',type:'string'},
             {name:'b',type:'string'},
             {name:'c',type:'string'},
             {name:'d',type:'string'},
             {name:'e',type:'string'},
             {name:'f',type:'string'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/employecontrol/employeinsure_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: false
    });
    
    //校区combox
    var companyComboboxForm= Ext.create('Ext.form.field.ComboBox', {
        id:'company_id',
	   	name:'company_id',
       	fieldLabel: '校区', 
       	allowBlank : false,
       	editable:false,
       	emptyText: '请选择',
       	store: new Ext.data.Store({ 
           	singleton : true, 
	   	    proxy: {
	   	        type: 'ajax',
	   	        url: '/resourcecontrol/company_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'cn_name'], 
           autoLoad:true 
       	}), 
       	loadingText:'正在加载数据，请稍侯……', 
       	triggerAction:'all', 
       	valueField:'id', 
       	displayField:'cn_name'
    });
    
    //员工combox
    var employeComboBox= Ext.create('Ext.form.field.ComboBox', { 
       id : 'employe_combobox_id',
	   name:'employe_id',
       fieldLabel: '员工', 
       typeAhead:true,
       minChars:1,
       editable:true,
       allowBlank : false,
       blankText : '请选择员工	.',
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/employecontrol/employe_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name',
       listeners: {
    		'select': function(combo, records, opts) {	
   	    		//alert("待增加学生查找") ;
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
    
    
    //多选
    var selModel = Ext.create('Ext.selection.CheckboxModel');
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        //selModel:selModel,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 80, dataIndex: 'id', sortable: false,hidden : true},
			{text: "月份", width: 80, dataIndex: 'month', sortable: false},
			{text: "员工ID", width: 80, dataIndex: 'employe_id', sortable: false,hidden : true},
			{text: "员工", width: 80, dataIndex: 'employe__name', sortable: false},
			{text: "养老", width: 80, dataIndex: 'a', sortable: false},
			{text: "医疗", width: 80, dataIndex: 'b', sortable: false},
			{text: "失业", width: 80, dataIndex: 'c', sortable: false},
			{text: "工伤", width: 80, dataIndex: 'd', sortable: false},
			{text: "生育", width: 80, dataIndex: 'e', sortable: false},
			{text: "个人所得税", width: 80, dataIndex: 'f', sortable: false}
			
        ],
        //title: '考勤管理',
        // disableSelection: false,
        layout : 'fit',
        //frame:true, 取消边框
        loadMask:true,
        region: 'north',
        //renderTo: 'employee-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	form.getForm().reset();
        			var selected = grid.getSelectionModel().getSelection();
					form.loadRecord(selected[0]);
        			win.show();
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		xtype: 'combobox',
        		id:'company_combox_id',
            	name:'company',
            	labelWidth: 40,
            	width: 140,
               	fieldLabel: '校区',
               	emptyText: '请选择校区',
               	editable:false,
               	store: new Ext.data.Store({ 
                   	singleton : true, 
        	   	    proxy: {
        	   	        type: 'ajax',
        	   	        url: '/resourcecontrol/company_select/',
        	   	        actionMethods:'post',
        	   	        reader: {
        	   	            type: 'json'
        	   	        }
        	   	    },
                   fields:['id', 'cn_name'], 
                   autoLoad:true 
               	}), 
               	loadingText:'正在加载数据，请稍侯……', 
               	triggerAction:'all', 
               	valueField:'id', 
               	displayField:'cn_name'        		
        	},{
        		xtype:'button',
        		text:'查询',
        		iconCls:'search', 
        		handler:function(){
//	        		if(Ext.getCmp('company_combox_id').getValue()!=null){
//	        			params = { company_id:Ext.getCmp('company_combox_id').getValue() };
//	        		    Ext.apply(store.proxy.extraParams, params);
//	        		    store.currentPage=1;
//	        		    store.load();
//	    			}else{
//	    				Ext.Msg.alert('系统提示', '请选择校区!');
//	    			}
        			params = { company_id:Ext.getCmp('company_combox_id').getValue() };
        		    Ext.apply(store.proxy.extraParams, params);
        		    store.currentPage=1;
        		    store.load();
        		}
        	},{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			form.getForm().reset();
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				form.getForm().reset();
						form.loadRecord(selected[0]);
	        			win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的记录!');
	    			}
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
	        					var ids="";
	                			Ext.each(selected, function (item) {
	                				ids+=item.data.id+",";
	                			});
	                			Ext.Ajax.request({
	                			    url:'/employecontrol/employeinsure_del/',
	                			    method : 'post',
	                			    params:{
	                			    	ids:ids
	                			    },
	                			    success:function(response){
	                			        var results =  Ext.decode(response.responseText);
	                			        if(results.success){
	                			        	store.load();
	                			        }else{
	                			        	Ext.Msg.alert('系统提示', results.message);
	                			        }
	                			    }
	                			});
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要删除的记录!');
	    			}
        			
        		}
        	}]
        }]
    });
    

	var form = Ext.create('Ext.form.Panel', {
		width: 550,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
				xtype:'hiddenfield',
				fieldLabel:'id',
				name:'id'
			},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[employeComboBox,{
				xtype:'datefield',
				editable:false,
				fieldLabel:'月份',
				allowBlank : false,
				format: 'Y-m-d',
				name:'month',
				blankText : '请输入月份	.'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '养老',
				name : 'a',
				allowBlank : false,
				value:'0',
				blankText : '请输入养老金额		.'
			}, {
				fieldLabel : '医疗',
				name : 'b',
				allowBlank : false,
				value:'0',
				blankText : '请输入医疗金额    .'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '失业',
				name : 'c',
				value:'0',
				allowBlank : false,
				blankText : '请输入失业金额   .'
			}, {
				fieldLabel : '工伤',
				name : 'd',
				value:'0',
				blankText : '请输入工伤金额	. ',
				allowBlank : false
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '生育',
				name : 'e',
				value:'0',
				blankText : '请输入生育金额	. ',
				allowBlank : false
			}, {
				fieldLabel : '个人所得税  .',
				name : 'f',
				value:'0',
				blankText : '请输入个人所得税金额	. ',
				allowBlank : false
			}]
		}]
	});

	var win = Ext.create('Ext.window.Window', {
		//title : '---',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/employecontrol/employeinsure_save/',
						success : function(form, action) {
							//Ext.Msg.alert('系统提示',  action.result.message);
							//form.getForm().reset();
							win.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});    
    
	// 适应屏幕
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'insure-grid',
		items:[grid]
	});
	
});